package com.ws.game.lesson3.fire
{
	import com.ws.game.common.framework.BaseCtrl;
	import com.ws.game.common.framework.BaseGameObject;
	import com.ws.game.common.global.Global;
	import com.ws.game.common.utils.MoveDirectionType;
	import com.ws.log.WSLog;
	
	import flash.utils.getQualifiedClassName;
	
	public class ActionObject extends BaseGameObject
	{
		protected var speed:Number = 1.2;
		
		protected var walkDirection:uint=MoveDirectionType.STOP;
		protected var ctrl:BaseCtrl;
		protected var _isWalk:Boolean = false;		
		protected var _hitPoint:Array = [[1,1],[25,1],[49,1],[49,25],[49,49],[25,49],[1,49],[1,25]];
		
		public function ActionObject(aCtrl:BaseCtrl)
		{
			super();
			this.ctrl = aCtrl;
			this.ctrl.target = this;			
			this._hitTest = true;
//			genHitTestPoint();
		}
		
		public function set direction(dir:uint):void
		{
			walkDirection = dir;
		}
		
		public function get direction():uint
		{
			return walkDirection;
		}
		
		public function set isWalk(value:Boolean):void
		{
			_isWalk = value;
		}
		
		protected function move():void
		{			
			if (!nextCanMove) {
				return; 
			}
			switch(walkDirection)
			{
				case MoveDirectionType.UP:
					y -= speed;
					break;
				case MoveDirectionType.DOWN:
					y += speed;
					break;
				case MoveDirectionType.LEFT:
					x -= speed;
					break;
				case MoveDirectionType.RIGHT:
					x += speed;
					break;
				default:
					break;
			}
		}
		
		public function get nextCanMove():Boolean
		{
			var nx:uint = x;
			var ny:uint = y;
			// 根据移动方向进行处理，计算出下一目标点位置
			switch(walkDirection)
			{
				case MoveDirectionType.UP:
					ny = y-speed;
					break;
				case MoveDirectionType.DOWN:
					ny = y+speed;
					break;
				case MoveDirectionType.LEFT:
					nx = x-speed;
					break;
				case MoveDirectionType.RIGHT:
					nx = x+speed;
					break;
				default:break;
			}
			
			// 如果下一目标点超出屏幕范围，则不能移动
			if (nx > Global.stage.stageWidth - width || nx < 0) 
				return false;
			if (ny > Global.stage.stageHeight - height || ny < 0)
				return false;
			
			for each(var obj:BaseGameObject in Global.scene.gObjects)
			{
				if (obj == this || !obj.hitTest){
					continue;
				}
				for each(var p:Array in _hitPoint){
					if (obj.hitTestPoint(nx + p[0], ny + p[1], true)){		
						return false;
					}
				}
			}
			
			// 检测通过
			return true;
		}
		
		override public function doWork():void
		{
			if (_isWalk) 
				move();
			super.doWork();
		}
		
		override public function dispose():void{
			this.ctrl.dispose();
			Global.scene.removeGameObject(this);
		}
		
//		private function genHitTestPoint():void{
//			var htp:Array =new Array();
//			htp.push([0,0]);
//			htp.push([this.width/2,0]);
//			htp.push([this.width,0]);
//			htp.push([this.width,this.height/2]);
//			htp.push([this.width,this.height]);
//			htp.push([this.width/2,this.height]);
//			htp.push([0,this.height]);
//			htp.push([0,this.height/2]);
//			this._hitPoint = htp;
//		}
	}
}